package com.elmcode.server.controller.admin;

import com.elmcode.common.utils.PageUtils;
import com.elmcode.common.utils.R;
import com.elmcode.server.constant.StatusConst;
import com.elmcode.server.dto.UserSaveInfo;
import com.elmcode.server.entity.UserEntity;
import com.elmcode.server.exception.ParamsException;
import com.elmcode.server.exception.UserException;
import com.elmcode.server.interceptor.LoginInterceptor;
import com.elmcode.server.properties.JWTProperties;
import com.elmcode.server.service.UserService;
import com.elmcode.server.utils.JWTUtil;
import com.elmcode.server.utils.Result;
import com.elmcode.server.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;



/**
 * 员工
 *
 * @author laozj
 * @email laozj@elm.com
 * @date 2024-06-13 13:05:04
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("admin/user")
public class UserController {

    @Autowired
    private JWTProperties jwtProperties;

    @Autowired
    private UserService userService;

    /**
     * 登录
     * /admin/user/login
     */
    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public Result login(String loginName,String password) throws ParamsException {
        if(StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)){
            throw new ParamsException("账号或者密码不能为空");
        }
        UserVo user = userService.login(loginName,password);
        if(user == null){
            return Result.error("账号或者密码错误");
        }
        if(user.getStatus().equals(StatusConst.INACTIVE_STATUS)){
            return Result.error("账号被冻结");
        }

        /**
         * 生成token
         */
        Map<String,Object> payload = new HashMap<>();
        payload.put(JWTUtil.USER_ID,user);
        String token = JWTUtil.generateToken(payload, jwtProperties.getJwtKey(), jwtProperties.getJwtTokenExpire());

        Map<String,Object> data = new HashMap<>();
        data.put("token",token);
        data.put("userInfo",user);

        return Result.ok("登录成功",data);
    }

    /**
     * 添加用户
     */
    @ApiOperation("添加用户接口")
    @PostMapping
    public Result addUser(UserSaveInfo userSaveInfo) throws UserException {
        userService.saveUser(userSaveInfo);
        return Result.ok("添加成功");
    }

    /**
     * 查询用户
     */
    @GetMapping
    @ApiOperation("查询用户接口")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);
        return Result.ok("获取用户成功",page.getList(),page.getTotal());
    }

    /**
     * 更新/编辑用户接口
     */
    @ApiOperation("更新/编辑用户接口")
    @PutMapping("/{id}")
    public Result update(@PathVariable("id")Long id,UserSaveInfo userSaveInfo){
        userService.updateUserInfo(id,userSaveInfo);

        return Result.ok("更新成功");
    }

    /**
     * 删除用户接口
     */
    @ApiOperation("删除用户接口")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id")Long id){
        userService.deleteById(id);

        return Result.ok("删除成功");
    }


    /**
     * 禁用/启用用户接口
     */
    @ApiOperation("禁用/启用用户接口")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable("id")Long id,@RequestParam String status) throws ParamsException {
        userService.updateStatus(id,status);

        return Result.ok("更新状态成功");
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        UserEntity user = userService.getById(id);

        return R.ok().put("data",user);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok("保存成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));
        return R.ok("删除成功");
    }

}
